package com.jk.controller;

import com.jk.dto.JsonResult;
import com.jk.entity.account.AccTheme;
import com.jk.entity.account.AccUser;
import com.jk.service.account.AccThemeService;
import com.jk.service.account.AccUserService;
import com.jk.util.JsonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by yanghuan on 2015/7/21.
 */
@Controller
@RequestMapping(value = "/")
public class LoginController {
    @Autowired
    private AccUserService accUserService;
    @Autowired
    private AccThemeService accThemeService;

    @RequestMapping(value = "doLogin")
    @ResponseBody
    public String doLogin(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject currentUser = SecurityUtils.getSubject();
        try {
            currentUser.login(token);
            //验证是否成功登录的方法
            if (currentUser.isAuthenticated()) {
                AccTheme accTheme = accThemeService.select();
                Session session = currentUser.getSession();
                session.setAttribute("accTheme", accTheme);
                return JsonUtil.toString(new JsonResult(200, "登录成功"));
            } else {
                return JsonUtil.toString(new JsonResult(201, "登录失败"));
            }
        } catch (UnknownAccountException ex) {
            return JsonUtil.toString(new JsonResult(201, "用户名密码不匹配"));
        } catch (IncorrectCredentialsException e) {
            return JsonUtil.toString(new JsonResult(201, "用户名密码不匹配"));
        } catch (AuthenticationException e) {
            return JsonUtil.toString(new JsonResult(201, "其他的登录错误"));
        }
    }

    @RequestMapping(value = "login")
    public String login() {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            return "index";
        }
        return "login/login";
    }

    @RequestMapping(value = "logout")
    public String logout() {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        return "redirect:login";
    }

    @RequestMapping(value = "toChangePwd")
    public String toChangePwd() {
        return "login/changePwd";
    }

    @RequestMapping(value = "changePwd")
    @ResponseBody
    public String changePwd(String password) {
        Subject currentUser = SecurityUtils.getSubject();
        try {
            AccUser accUser = (AccUser) currentUser.getSession().getAttribute("currentUser");
            accUser.setAccUserPassword(password);
            accUserService.updateByPrimaryKey(accUser);
            return JsonUtil.toString(new JsonResult(201, "修改成功"));
        } catch (Exception e) {
            return JsonUtil.toString(new JsonResult(201, "修改失败"));
        }
    }

    @RequestMapping(value = "index")
    public String index() {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            return "index";
        }
        return "login/login";
    }

    @RequestMapping(value = "noPermission")
    public String noPermission() {
        return "error/noPermission";
    }
}
